﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.LogicalGroups.CUiLGRExportGroupsToExcelManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using System.Xml;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.LogicalGroups;

internal class CUiLGRExportGroupsToExcelManager
{
  private string m_selectionFieldsXmlStr = string.Empty;
  private const string SelectionFieldsStorageKey = "ExportGroupsFieldsXml";
  private ArrayList m_planningFieldTypes = new ArrayList(21);
  private string[] m_classTypesNames;
  private const string PmNode = "PmNode";
  private Dictionary<string, List<CApLGRExcelReportColumnData>> m_classFields;
  private CApLGRExcelReportManager m_reportWriter;
  private ITxCommand m_command;

  internal CUiLGRExportGroupsToExcelManager(ITxCommand curCommand)
  {
    this.m_reportWriter = new CApLGRExcelReportManager();
    this.m_classFields = new Dictionary<string, List<CApLGRExcelReportColumnData>>();
    this.m_command = curCommand;
    this.InitClassTypes();
    this.InitFieldTypes();
  }

  public void ExportGroups()
  {
    if (TxApplication.PlatformType == 2 || TxApplication.PlatformType == 4 || TxApplication.PlatformType == 3)
      this.ExportOnOfflinePlatform();
    else
      this.StartRegularExport();
  }

  private void InitFieldTypes()
  {
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 1);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 10);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 21);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 22);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 5);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 14);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 4);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 13);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 19);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 20);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 2);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 11);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 3);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 12);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 6);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 15);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 8);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 17);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 7);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 16 /*0x10*/);
    this.m_planningFieldTypes.Add((object) (TxPlanningFieldType) 999);
  }

  private void InitClassTypes()
  {
    switch ((int) TxApplication.PlatformType)
    {
      case 0:
      case 4:
        this.m_classTypesNames = new string[2]
        {
          "ItemRevision",
          "MEWorkareaRevision"
        };
        break;
      case 1:
        this.m_classTypesNames = new string[3]
        {
          "PmNode",
          "PmPart",
          "PmResource"
        };
        break;
      case 2:
      case 3:
        this.m_classTypesNames = new string[3]
        {
          "PmNode",
          "PmPart",
          "PmResource"
        };
        break;
    }
  }

  private void ExportOnOfflinePlatform()
  {
    this.CreateExcelReportFile(this.GetGroupsToExport(), new List<CApLGRExcelReportColumnData>());
  }

  private void StartRegularExport()
  {
    if (!this.CustomizeFields())
      return;
    TxObjectList groupsToExport = this.GetGroupsToExport();
    this.ImportObjectsFields(groupsToExport);
    List<CApLGRExcelReportColumnData> columns = new List<CApLGRExcelReportColumnData>();
    foreach (KeyValuePair<string, List<CApLGRExcelReportColumnData>> classField in this.m_classFields)
      columns.AddRange((IEnumerable<CApLGRExcelReportColumnData>) classField.Value);
    this.CreateExcelReportFile(groupsToExport, columns);
  }

  private bool CustomizeFields()
  {
    bool flag = false;
    this.LoadAttributesListFromAppData();
    TxFieldsSelectionData selectionData = new TxFieldsSelectionData();
    if (this.ShowCustomizationForm(selectionData) == DialogResult.OK)
    {
      this.SaveSelectedProperties(selectionData);
      flag = true;
    }
    return flag;
  }

  private DialogResult ShowCustomizationForm(TxFieldsSelectionData selectionData)
  {
    this.AddCustomizationData(selectionData);
    selectionData.CurrentType = this.m_classTypesNames[0];
    return new TxFieldsSelectionForm(selectionData).ShowDialog(new TxFieldsSelectionDialogData()
    {
      DialogTitle = LGRLogicalGroupResource.EXPORT_GROUPS_PROPERTIES_TITLE
    });
  }

  private void AddCustomizationData(TxFieldsSelectionData selectionData)
  {
    foreach (string classTypesName in this.m_classTypesNames)
    {
      if (!selectionData.BaseTypeMetaData.ContainsBaseType(classTypesName))
      {
        TxFieldsSelectionBaseTypeMetaData baseTypeMetaData = new TxFieldsSelectionBaseTypeMetaData();
        baseTypeMetaData.FieldsConfiguration = !(classTypesName == "PmNode") ? (TxFieldsSelectionBaseTypeMetaData.TxFieldsConfiguration) 2 : (TxFieldsSelectionBaseTypeMetaData.TxFieldsConfiguration) 3;
        foreach (TxPlanningFieldType planningFieldType in this.m_planningFieldTypes)
          baseTypeMetaData.FieldsFilter.AddIncludedType(planningFieldType);
        selectionData.BaseTypeMetaData.Add(classTypesName, baseTypeMetaData);
      }
    }
    selectionData.SelectionFields.LoadXml(this.m_selectionFieldsXmlStr);
    selectionData.DefaultTypeFieldSelection = (TxFieldsSelectionData.TxDefaultTypeFieldSelection) 0;
    selectionData.SortOrder = (TxFieldsSelectionData.TxSortOrder) 1;
  }

  private void SaveSelectedProperties(TxFieldsSelectionData selectionData)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
      this.m_selectionFieldsXmlStr = selectionData.SelectionFields.ToXmlString();
      this.SaveAttributesListToAppData();
      this.CreateReportColumns(selectionData.SelectionFields);
    }
    catch (XmlException ex)
    {
    }
    finally
    {
      Cursor.Current = current;
    }
  }

  private void CreateReportColumns(
    TxSelectionFieldsDictionary txSelectionFieldsDictionary)
  {
    int index = 1;
    foreach (string classTypesName in this.m_classTypesNames)
    {
      if (txSelectionFieldsDictionary.ContainsBaseType(classTypesName))
      {
        foreach (TxSelectionField selectionField in txSelectionFieldsDictionary.SelectionFields(classTypesName))
        {
          CApLGRExcelReportColumnData reportColumnData = new CApLGRExcelReportColumnData(index, classTypesName, selectionField);
          if (!this.m_classFields.ContainsKey(selectionField.ClassTypeName))
            this.m_classFields.Add(selectionField.ClassTypeName, new List<CApLGRExcelReportColumnData>(1)
            {
              reportColumnData
            });
          else
            this.m_classFields[selectionField.ClassTypeName].Add(reportColumnData);
          ++index;
        }
      }
    }
  }

  private bool LoadAttributesListFromAppData()
  {
    bool flag = false;
    if (this.m_command != null)
    {
      try
      {
        this.m_selectionFieldsXmlStr = new TxCommandPrivateDataStorage(this.m_command).GetStringValue("ExportGroupsFieldsXml");
        flag = true;
      }
      catch (TxCommandPrivateDataValueNotStoredException ex)
      {
      }
    }
    return flag;
  }

  private void SaveAttributesListToAppData()
  {
    if (this.m_command == null)
      return;
    try
    {
      new TxCommandPrivateDataStorage(this.m_command).SetStringValue("ExportGroupsFieldsXml", this.m_selectionFieldsXmlStr);
    }
    catch (TxException ex)
    {
    }
  }

  private TxObjectList GetGroupsToExport()
  {
    TxObjectList groupsToExport = CApLGRLogicalGroupsUtil.GetLogicalGroupsFromSelection();
    if (groupsToExport == null || ((Collection<ITxObject>) groupsToExport).Count == 0)
      groupsToExport = CApLGRLogicalGroupsUtil.GetAllLogicalGroups();
    return groupsToExport;
  }

  private void ImportObjectsFields(TxObjectList logGroups)
  {
    TxObjectList objectsToExport = this.GetObjectsToExport(logGroups);
    if (((Collection<ITxObject>) objectsToExport).Count <= 0)
      return;
    this.ImportFields(objectsToExport);
  }

  private TxObjectList GetObjectsToExport(TxObjectList logGroups)
  {
    TxObjectList objectsToExport = new TxObjectList();
    TxTypeFilter txTypeFilter = new TxTypeFilter();
    txTypeFilter.AddIncludedType(typeof (ITxLogicalChild));
    txTypeFilter.AddExcludedType(typeof (TxLogicalGroup));
    txTypeFilter.AddExcludedType(typeof (ITxSection));
    foreach (ITxObject logGroup in (Collection<ITxObject>) logGroups)
    {
      foreach (ITxObject allDescendant in (Collection<ITxObject>) (logGroup as TxLogicalGroup).GetAllDescendants((ITxTypeFilter) txTypeFilter))
      {
        if (!CApLGRLogicalGroupsUtil.IsSubComponent(allDescendant) && !((Collection<ITxObject>) objectsToExport).Contains(allDescendant))
          ((Collection<ITxObject>) objectsToExport).Add(allDescendant);
      }
    }
    return objectsToExport;
  }

  private bool ImportFields(TxObjectList objectsToExport)
  {
    bool flag = false;
    ITxPlatformGlobalServicesProvider servicesProvider = TxApplication.ActiveDocument.PlatformGlobalServicesProvider;
    if (servicesProvider != null && servicesProvider.IsPlanningTypeMetaDataInitialized())
    {
      ArrayList fieldsToImport = this.GetFieldsToImport();
      TxImportFieldsByClassData fieldsByClassData = new TxImportFieldsByClassData();
      fieldsByClassData.Objects = objectsToExport;
      fieldsByClassData.addFieldsToCollect(fieldsToImport);
      try
      {
        servicesProvider.ImportFields((TxPlatformImportFieldsData) fieldsByClassData);
        flag = true;
      }
      catch (TxImportFieldsInvalidFieldsToCollectException ex)
      {
      }
    }
    return flag;
  }

  private ArrayList GetFieldsToImport()
  {
    ArrayList fieldsToImport = new ArrayList();
    foreach (KeyValuePair<string, List<CApLGRExcelReportColumnData>> classField in this.m_classFields)
    {
      TxClassFieldsCollectionData fieldsCollectionData = new TxClassFieldsCollectionData();
      fieldsCollectionData.ClassName = classField.Key;
      ArrayList fieldNames = this.GetFieldNames(classField.Value);
      fieldsCollectionData.FieldNames = fieldNames;
      fieldsToImport.Add((object) fieldsCollectionData);
    }
    return fieldsToImport;
  }

  private ArrayList GetFieldNames(List<CApLGRExcelReportColumnData> fieldColumns)
  {
    ArrayList fieldNames = new ArrayList(fieldColumns.Count);
    foreach (CApLGRExcelReportColumnData fieldColumn in fieldColumns)
      fieldNames.Add((object) fieldColumn.FieldName);
    return fieldNames;
  }

  private bool CreateExcelReportFile(
    TxObjectList logGroups,
    List<CApLGRExcelReportColumnData> columns)
  {
    bool excelReportFile = false;
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
      excelReportFile = this.m_reportWriter.InitReport(columns.Count);
      if (excelReportFile)
      {
        this.m_reportWriter.SetVisibility(false);
        this.m_reportWriter.WriteHeader(columns);
        foreach (TxLogicalGroup logGroup in (Collection<ITxObject>) logGroups)
        {
          this.m_reportWriter.AddObject((object) logGroup.Name, logGroup.Name, 0, true);
          this.AddCollectionToReport((ITxObjectCollection) logGroup, logGroup.Name, 0);
        }
        this.m_reportWriter.Allign();
        excelReportFile = this.m_reportWriter.SetVisibility(true);
        if (excelReportFile)
          this.m_reportWriter.Release();
        else
          this.m_reportWriter.Quit();
      }
      else
        this.m_reportWriter.Quit();
    }
    catch (Exception ex)
    {
      this.m_reportWriter.Quit();
      excelReportFile = false;
    }
    finally
    {
      Cursor.Current = current;
    }
    return excelReportFile;
  }

  private void AddCollectionToReport(ITxObjectCollection collection, string hierarchy, int indent)
  {
    if (collection == null)
      return;
    foreach (ITxObject directDescendant in (Collection<ITxObject>) collection.GetDirectDescendants((ITxTypeFilter) new TxNoTypeFilter()))
    {
      if (directDescendant is TxLogicalGroup)
      {
        string hierarchyName = CApLGRLogicalGroupsUtil.ConcatStringsWithDot(hierarchy, directDescendant.Name);
        this.m_reportWriter.AddObject((object) directDescendant.Name, hierarchyName, indent + 1, true);
      }
      else
        this.m_reportWriter.AddObject((object) directDescendant.Name, hierarchy, indent + 1, false);
      ITxPlanningObject planningRepresentation = CApLGRLogicalGroupsUtil.GetPlanningRepresentation(directDescendant);
      if (TxApplication.PlatformType != 3 && planningRepresentation != null)
      {
        List<CApLGRExcelReportColumnData> typeColumns = this.GetTypeColumns(planningRepresentation);
        string[] objectFields = this.GetObjectFields(typeColumns);
        this.m_reportWriter.WriteAttributes(planningRepresentation.GetFields(objectFields, true), typeColumns);
      }
      if (directDescendant is ITxCompound)
        this.AddCollectionToReport(directDescendant as ITxObjectCollection, hierarchy, indent + 1);
      else if (directDescendant is TxLogicalGroup)
      {
        string hierarchy1 = CApLGRLogicalGroupsUtil.ConcatStringsWithDot(hierarchy, directDescendant.Name);
        this.AddCollectionToReport(directDescendant as ITxObjectCollection, hierarchy1, indent + 1);
      }
    }
  }

  private string[] GetObjectFields(List<CApLGRExcelReportColumnData> classColumns)
  {
    string[] objectFields = new string[classColumns.Count];
    int index = 0;
    foreach (CApLGRExcelReportColumnData classColumn in classColumns)
    {
      objectFields[index] = classColumn.FieldName;
      ++index;
    }
    return objectFields;
  }

  private List<CApLGRExcelReportColumnData> GetTypeColumns(ITxPlanningObject obj)
  {
    List<CApLGRExcelReportColumnData> typeColumns = new List<CApLGRExcelReportColumnData>();
    TxPlanningTypeMetaData typeMetaData = CApLGRLogicalGroupsUtil.GetTypeMetaData(obj.PlanningType);
    if (typeMetaData != null)
    {
      foreach (KeyValuePair<string, List<CApLGRExcelReportColumnData>> classField in this.m_classFields)
      {
        if (typeMetaData.IsDerivedFromPlanningType(classField.Key))
          typeColumns.AddRange((IEnumerable<CApLGRExcelReportColumnData>) classField.Value);
      }
    }
    return typeColumns;
  }
}
